package com.aube.interact.model;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huyaonan on 16/3/23.
 */
public class GoodsModel extends BaseModel {

    public List<GoodsItem> goodsidList;

    public float percentageX=0;
    public float percentageY=0;
    public int startSeconds=0;
    public int endSeconds=0;
    public String beforeDesc;
    public String afterDesc;

    public List<GoodsRoute> circleTrajectoryList;
    public List<GoodsRoute> rectangleTrajectoryList;

    //手动新加的id
    public String rid="";

    public List<GoodsItem> getGoods() {
        return goodsidList;
    }

    public static GoodsModel parse(JSONObject jsonObject) {
        GoodsModel model = new GoodsModel();
        model.percentageX = (float) jsonObject.optDouble("percentageX");
        model.percentageY = (float) jsonObject.optDouble("percentageY");
        model.startSeconds = jsonObject.optInt("startSeconds");
        model.endSeconds = jsonObject.optInt("endSeconds");
        model.beforeDesc = jsonObject.optString("beforeDesc");
        model.afterDesc = jsonObject.optString("afterDesc");
        model.rid = jsonObject.optString("rid");
        JSONArray circleArray = jsonObject.optJSONArray("circleTrajectoryList");
        if(circleArray != null) {
            List<GoodsRoute> circleRoutes = new ArrayList<>();
            for(int i=0; i<circleArray.length(); i++)
                circleRoutes.add(GoodsRoute.parse(circleArray.optJSONObject(i)));
            model.circleTrajectoryList = circleRoutes;
        }
        JSONArray rectangleArray = jsonObject.optJSONArray("rectangleTrajectoryList");
        if(rectangleArray != null) {
            List<GoodsRoute> rectangleRoutes = new ArrayList<>();
            for(int i=0; i<rectangleArray.length(); i++)
                rectangleRoutes.add(GoodsRoute.parse(rectangleArray.optJSONObject(i)));
            model.rectangleTrajectoryList = rectangleRoutes;
        }
        JSONArray goodsArray = jsonObject.optJSONArray("goodsidList");
        if(goodsArray != null) {
            List<GoodsItem> goodsItems = new ArrayList<>();
            for(int i=0; i<goodsArray.length(); i++)
                goodsItems.add(GoodsItem.parse(goodsArray.optJSONObject(i)));
            model.goodsidList = goodsItems;
        }
        return model;
    }

    /**
     * for test
     * @return
     */
    public String getId() {
        if(goodsidList == null || goodsidList.size() == 0)
            return "";
        return goodsidList.get(0)._id;
    }

    public String getName() {
        if(goodsidList == null || goodsidList.size() == 0)
            return "";
        return goodsidList.get(0).tlTitle;
    }

    public int getSize() {
        if(goodsidList == null)
            return 0;
        return goodsidList.size();
    }

    

}
